<template>
  <!-- 收藏组件 -->
  <van-icon :color="collected ? 'yellow' : '#777' " :name="collected ? 'star' : 'star-o' " @click="onCollected" />
</template>

<script>
import { addArticleCollected, deleteArticleCollected } from '@/api/article.js'
export default {
  data () {
    return {}
  },
  model: {
    prop: 'collected',
    event: 'isCollected'
  },
  props: {
    // 收藏状态
    collected: {
      type: Boolean,
      require: true
    },
    // 文章id
    articleId: {
      type: [Number, String],
      require: true
    }
  },
  methods: {
    // 点击收藏
    async onCollected () {
      this.$toast.loading({
        message: '操作中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.collected) {
          // 取消收藏
          await deleteArticleCollected(this.articleId)
          this.$toast('取消收藏成功')
        } else {
          // 收藏
          await addArticleCollected(this.articleId)
          this.$toast('收藏成功')
        }
        this.$emit('isCollected', !this.collected)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
